/**
 * 
 */
package net.freestu.sola.web.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import net.freestu.sola.web.global.GlobleVar;

public class MyLogger {

	public static void log(String str) {
		printToScreen(str);//这里为了避免向终端输出时浪费时间.
		//而在调试的时候必须用这行代码.
		printToFile(str);

	}

	public static void log(Exception e) {

		printToScreen(e.toString());
		printToFile(e.toString());
	}

	private synchronized static void printToScreen(String str) {

		System.out.println("[" + "Spider:" + Thread.currentThread().getName()
				+ "--->" + str + "]");
	}

	private synchronized static void printToFile(String str) {

		BufferedWriter bw = null;
		try {

			FileWriter w = new FileWriter(GlobleVar.EXEFOLDER + File.separator
					+ "Log", true);
			//检查覆盖.这个是清除所有的东西再往里面写入东西。
			bw = new BufferedWriter(w);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Be not able to find the log file.");
		}
		
		try {
			String a = "Spider:" + Thread.currentThread().getName() + "--->";
			str = a + str;
			bw.write(str);
			bw.newLine();
			bw.close();
		} catch (IOException e) {

			e.printStackTrace();

			return;
		}
	}

	public static void main(String[] args) {

		MyLogger.log("Don't you konw I love you!");
	}
}